package com.vains.handler;

import com.vains.bean.Result;
import com.vains.constant.HttpStatusConstants;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author vains
 * @date 2021/4/3 17:57
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 权限不足异常
     * @param e 异常实例
     * @return Result
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result<OAuth2Exception> accessDeniedException(AccessDeniedException e) {
        String message = "权限不足.";
        OAuth2Exception exception = OAuth2Exception.create(OAuth2Exception.ACCESS_DENIED, e.getMessage());
        return Result.oauth2Error(HttpStatusConstants.FORBIDDEN, message, exception);
    }

}
